package cn.scw.project.goods.service.impl;

import cn.scw.common.constant.DictTypeConsts;
import cn.scw.common.exception.CustomException;
import cn.scw.common.utils.StringUtils;
import cn.scw.common.utils.bean.ToolUtils;
import cn.scw.common.utils.sql.JpaUtil;
import cn.scw.project.goods.domain.GCommodityType;
import cn.scw.project.goods.domain.netework.bean.GCommodityTypeAddReq;
import cn.scw.project.goods.domain.netework.bean.GCommodityTypeListReq;
import cn.scw.project.goods.domain.vo.GCommodityTypeVo;
import cn.scw.project.goods.mapper.GCommodityTypeMapper;
import cn.scw.project.goods.service.IGCommodityTypeService;
import cn.scw.project.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品详细分类Service业务层处理
 *
 * @author api
 * @date 2020-05-25
 */
@Service
public class GCommodityTypeServiceImpl implements IGCommodityTypeService {
    @Autowired
    private GCommodityTypeMapper gCommodityTypeMapper;
    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 查询商品详细分类
     *
     * @param id 商品详细分类ID
     * @return 商品详细分类
     */
    @Override
    public GCommodityType selectGCommodityTypeById(Long id) {
        return gCommodityTypeMapper.selectGCommodityTypeById(id);
    }

    /**
     * 查询商品详细分类列表
     *
     * @param req 商品详细分类
     * @return 商品详细分类
     */
    @Override
    public List<GCommodityTypeVo> selectGCommodityTypeList(GCommodityTypeListReq req) {
        req.setLike(JpaUtil.getLikeStr(req.getLike()));
        return gCommodityTypeMapper.selectGCommodityTypeList(req);
    }

    /**
     * 新增商品详细分类
     *
     * @param req 商品详细分类
     * @return 结果
     */
    @Override
    public int insertGCommodityType(GCommodityTypeAddReq req) {
        if (StringUtils.isBlank(req.getType()))
            throw new CustomException("请选择商品分类");
        if (!sysDictDataService.existsDictLabel(DictTypeConsts.G_FACILITY_TYPE, req.getType()))
            throw new CustomException("请选择正确的商品分类");
        if (StringUtils.isBlank(req.getName()))
            throw new CustomException("请输入分类名称");
        if (this.existsByTypeAndName(req.getType(), req.getName()))
            throw new CustomException("分类已存在");
        if (StringUtils.isBlank(req.getLitimgUrl()))
            throw new CustomException("请选择分类图片");

        GCommodityType gCommodityType=new GCommodityType();
        ToolUtils.copyPropertiesIgnoreNull(req,gCommodityType);
        return gCommodityTypeMapper.insertGCommodityType(gCommodityType);
    }

    /**
     * 修改商品详细分类
     *
     * @param req 商品详细分类
     * @return 结果
     */
    @Override
    public int updateGCommodityType(GCommodityTypeAddReq req) {
        GCommodityType gCommodityType = gCommodityTypeMapper.selectGCommodityTypeById(req.getId());
        if (!gCommodityType.getType().equals(req.getType())) {
            if (!sysDictDataService.existsDictLabel(DictTypeConsts.G_FACILITY_TYPE, req.getType()))
                throw new CustomException("请选择正确的商品分类");
        }
        if (!gCommodityType.getName().equals(req.getName())) {
            if (this.existsByTypeAndName(req.getType(), req.getName()))
                throw new CustomException("分类已存在");
        }
        ToolUtils.copyPropertiesIgnoreNull(req,gCommodityType);
        return gCommodityTypeMapper.updateGCommodityType(gCommodityType);
    }

    /**
     * 批量删除商品详细分类
     *
     * @param ids 需要删除的商品详细分类ID
     * @return 结果
     */
    @Override
    public int deleteGCommodityTypeByIds(Long[] ids) {
        return gCommodityTypeMapper.deleteGCommodityTypeByIds(ids);
    }

    /**
     * 删除商品详细分类信息
     *
     * @param id 商品详细分类ID
     * @return 结果
     */
    @Override
    public int deleteGCommodityTypeById(Long id) {
        return gCommodityTypeMapper.deleteGCommodityTypeById(id);
    }

    @Override
    public boolean existsByTypeAndName(String type, String name) {
        return gCommodityTypeMapper.existsByTypeAndName(type,name);
    }

    @Override
    public boolean existsByIdAndType(Long id, String facilityType) {
        return gCommodityTypeMapper.existsByIdAndType(id,facilityType);
    }
}
